package com.tjwx.util;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class txt2Nginx {

	public String subTxt(String str){
		return str.substring(str.lastIndexOf(":")+5,str.length());
	}

	public String subTxt2(String str){
		return str.substring(str.lastIndexOf("/")+1,str.length());
	}

	public String subIp(String str){
		return str.substring(7,26);
	}

	public void locationStr(){
		WriterOrReaderTxt t = new WriterOrReaderTxt();
		List<String> l = t.readTxt2list("/Users/yijon/IdeaProjects/test1/src/test/java/yijon/1.txt");
		for (int i = 0; i < l.size(); i++) {
			//System.out.println(l.get(i));
			System.out.println("location ^~ "+ subTxt(l.get(i))+" {");
			System.out.println("    "+"proxy_pass http://dingfu_app_"+ subTxt2(l.get(i))+";");
			System.out.println("}");
			System.out.println();
		}
	}

	public void upstreamStr(){
		WriterOrReaderTxt t = new WriterOrReaderTxt();
		List<String> l1 = t.readTxt2list("/Users/yijon/IdeaProjects/test1/src/test/java/yijon/1.txt");
		List<String> l2 = t.readTxt2list("/Users/yijon/IdeaProjects/test1/src/test/java/yijon/2.txt");
		List<String> l3 = t.readTxt2list("/Users/yijon/IdeaProjects/test1/src/test/java/yijon/3.txt");
		for (int i = 0; i < l1.size(); i++) {
			//System.out.println(l.get(i));
			System.out.println("upstream dingfu_app_"+ subTxt2(l1.get(i))+" {");
			System.out.println("    "+"server "+ subIp(l1.get(i))+";");
			System.out.println("    "+"server "+ subIp(l2.get(i))+";");
			System.out.println("    "+"server "+ subIp(l3.get(i))+";");
			System.out.println("}");
			System.out.println();
		}
	}

	public List<Map<String,String>> sgy(){
		WriterOrReaderTxt t = new WriterOrReaderTxt();
		//路径URL
		List<String> list1 = t.readTxt2list("E:\\ideaproject\\test-com.tjwx.service\\src\\main\\java\\com\\utral\\nlp\\com.tjwx.util\\1.txt");
		//参数in_arg
		List<String> list2 =  t.readTxt2list("E:\\ideaproject\\test-com.tjwx.service\\src\\main\\java\\com\\utral\\nlp\\com.tjwx.util\\2.txt");
		//服务名称servicename
		List<String> list3 = t.readTxt2list("E:\\ideaproject\\test-com.tjwx.service\\src\\main\\java\\com\\utral\\nlp\\com.tjwx.util\\3.txt");
		List<Map<String,String>> list = new ArrayList<>();
		for (int i = 0; i < list1 .size(); i++) {
			Map<String,String> map = new HashMap<String,String>();
			map.put("url",list1.get(i));
			map.put("in_arg",list2.get(i));
			map.put("serviceName",list3.get(i));
			list.add(map);
		}
		System.out.println("结果： "+ JSON.toJSONString(list));
		return list ;
	}

	public static void main(String[] args) {
		txt2Nginx x = new txt2Nginx();
//		x.locationStr();
//		System.out.println();
//		x.upstreamStr();
		x.sgy();
	}
}
